let App = getApp();

Page({
  data: {
    // 搜索框样式
    searchColor: "rgba(0,0,0,0.4)",
    searchSize: "15",
    searchName: "搜索商品",
    scrollHeight:0,
    // banner轮播组件属性
    indicatorDots: true, // 是否显示面板指示点	
    autoplay: true, // 是否自动切换
    interval: 3000, // 自动切换时间间隔
    duration: 800, // 滑动动画时长
    imgHeights: {}, // 图片的高度
    imgCurrent: {}, // 当前banne所在滑块指针

    // 页面元素
    items: {},
    data:{},
    list:{
      data:[]
    },
    newest: {},
    best: {},
    imgUrls:[
      {
        link: '/pages/index/index',
        url: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1551728114532&di=ffad27c308fc9dff013e4ad8f642a351&imgtype=0&src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20170731%2F41c57ef6582f4474b26a8ee5d68a4963_th.jpg'
      }
    ],
    scrollTop: 0,
    page: 1,
  },
  go: function () {
    wx.navigateTo({
      url: '../redpacket/index',
    })
  },
  onLoad: function() {
    let that = this
    // 设置页面标题
    // App.setTitle();
    // 设置navbar标题、颜色
    // App.setNavigationBar();
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
            scrollHeight: res.windowHeight
          });
          console.log("屏幕高度: " + res.windowHeight)
      }
  })
  },
  onShow: function(){
    // 获取首页数据
    this.setData({
      list:{
        data:[]
      }
    });
    console.log(1)
    this.getIndexData();
  },

  /**
   * 获取首页数据
   */
  getIndexData: function() {
    let _this = this;
    let dataList = this.data.list
    App._get('v1/goods/topList', {
      "page": _this.data.page
    }, function(result) {
      for(let i = 0;i<result.data.length;i++){
        dataList.data.push(result.data[i])
      }
      _this.data.list.data = dataList.data
    //  _this.data.list.data = dataList.data.concat(result.data)
     console.log(dataList)
      _this.setData({
        data:dataList.data
      });
    });
  },
  /**
   * 计算图片高度
   */
  imagesHeight: function(e) {
    let imgId = e.target.dataset.id,
      itemKey = e.target.dataset.itemKey,
      ratio = e.detail.width / e.detail.height, // 宽高比
      viewHeight = 750 / ratio, // 计算的高度值
      imgHeights = this.data.imgHeights;

    // 把每一张图片的对应的高度记录到数组里
    if (typeof imgHeights[itemKey] === 'undefined') {
      imgHeights[itemKey] = {};
    }
    imgHeights[itemKey][imgId] = viewHeight;
    // 第一种方式
    let imgCurrent = this.data.imgCurrent;
    if (typeof imgCurrent[itemKey] === 'undefined') {
      imgCurrent[itemKey] = Object.keys(this.data.items[itemKey].data)[0];
    }
    this.setData({
      imgCurrent
    });
  },

  bindChange: function(e) {
    let itemKey = e.target.dataset.itemKey,
      imgCurrent = this.data.imgCurrent;
    // imgCurrent[itemKey] = e.detail.current;
    imgCurrent[itemKey] = e.detail.currentItemId;
    this.setData({
      imgCurrent
    });
  },

  goTop: function(t) {
    this.setData({
      scrollTop: 0
    });
  },

  scroll: function(t) {
    console.log(t.detail.scrollTop)
    // this.setData({
    //   indexSearch: t.detail.scrollTop
    // }), t.detail.scrollTop > 300 ? this.setData({
    //   floorstatus: !0
    // }) : this.setData({
    //   floorstatus: !1
    // });
  },
  
  onShareAppMessage: function() {
    return {
      title: "小程序首页",
      desc: "",
      path: "/pages/index/index"
    };
  },

  /**
   * 下拉到底加载数据
   */
  onPullDownRefresh:function(){
    console.log("11111")
    App.showNavigationBarLoading() 
  },
  onReachBottom:function(){
    console.log("11111")
    App.showNavigationBarLoading() 
  },
  bindDownLoad: function () {
    console.log("11111")
    // 已经是最后一页
    ++this.data.page
    this.getIndexData();
  }

});